/*
 * Copyright 2007 by LongTop Corporation.
 * Softpack ChuangXin Building 15F, XiaMen, FuJian, PRC 361005
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 *
 */
package com.longtop.framework.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * 
 * @author IntelliWeb Team
 * @version 2.0
 * 
 */
public class DBUtil {
	private static DataSource ds = null;
	static {
		try {
			ds = (DataSource) SpringBeanFactory.getBean("dataSource");
		} catch (Exception ne) {
			ne.printStackTrace();
		}
	}

	/**
	 * get a connection for database access according to LMS configuration for database type.
	 */
	public static Connection getConnection() throws SQLException {
		Connection conn = null;
		try {
			if (ds != null) {
				conn = ds.getConnection();
			} else {
				SQLException sqlexcep = new SQLException("Can't get connection from DataSource");
				throw sqlexcep;
			}
		} catch (SQLException se) {
			throw se;
		}
		return conn;
	}

	// public static void main(String[] args) {
	// long begin = System.currentTimeMillis();
	// Connection conn = null;
	// Statement st = null;
	// ResultSet rs = null;
	// try{
	// conn = DBUtil.getConnection();
	// st = conn.createStatement();
	// rs = st.executeQuery("select * from iwb_frame_code_type");
	// while(rs.next()){
	// System.out.println(rs.getString(1));
	// }
	// }catch(SQLException e){
	// e.printStackTrace();
	// }finally{
	// try{
	// rs.close();
	// st.close();
	// conn.close();
	// }catch(Exception se){}
	// }
	// System.out.println(System.currentTimeMillis()-begin);
	// }
}
